package vashion.azeroth.common.im;

import com.google.common.collect.Lists;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;

import java.util.Collections;
import java.util.List;

/**
 * Created by xiaonan on 15/11/23.
 */
public class LoginSH256Util {

	public static final String domain = AppKeys.domain; //更换成你自己的 domain
	public static final String appToken  = AppKeys.appToken; //更换成你自己的appToken



	/***
	 * 根据appKey，appSecret 获取 登陆的对象。
	 * @param appKey
	 * @param appSecret
	 * @param openId
	 * @return
	 */
	public static ImSignModel getLoginModel(String appKey,
											String appSecret, Long openId) {
		String nonce = RandomStringUtils.randomAlphanumeric(6);
		Long timestamp = System.currentTimeMillis() / 1000;
		String signature = getSignature(appToken, appSecret, openId, nonce, timestamp);
		return new ImSignModel(domain, appKey, openId, nonce, timestamp, signature);
	}

	/***
	 * 获取签名
	 *
	 * @desc
	 * @param appToken 须从backyard或悟空IM管理人员获取到
	 * @param appSecret  须从backyard或悟空IM管理人员获取到
	 * @param openId      用户唯一标示
	 * @param nonce       随机数
	 * @param timestamp 当前以秒计的时间戳
	 * @return  签名
	 */
	public static String getSignature(String appToken, String appSecret, long openId, String nonce,
									  long timestamp) {
		List<String> list =
				Lists.newArrayList(appToken, appSecret, "" + openId, nonce, "" + timestamp);
		Collections.sort(list);
		String join = StringUtils.join(list, "");
		return new String(DigestUtils.sha256Hex(join));
	}

	/**
	 *
	 * @desc  检测签名是否合法
	 * @param appToken
	 * @param appSecret
	 * @param openId
	 * @param nonce
	 * @param timestamp
	 * @param singature
	 * @return
	 */
	public static boolean checkSingature(String appToken, String appSecret, long openId,
										 String nonce, long timestamp, String singature) {
		String sign = getSignature(appToken, appSecret, openId, nonce, timestamp);
		return (sign == null) ? false : (sign.equals(singature));
	}

	/**
	 * @desc
	 * 获取登陆对象  ，里面有各项免用户导入的数据
	 *
	 * @param domain
	 * @param appToken
	 * @param appKey   需要是  “端”  上使用的那个appKey ,也就是说这个最好有标示出是从什么端过来的appKey。或者请求过来的参数里带这个数据
	 * @param appSecret
	 * @param openId
	 * @return
	 */
	public static ImSignModel getLoginModel(String domain, String appToken, String appKey,
											String appSecret, Long openId) {
		String nonce = RandomStringUtils.randomAlphanumeric(6);
		Long timestamp = System.currentTimeMillis() / 1000;
		String signature = getSignature(appToken, appSecret, openId, nonce, timestamp);
		return new ImSignModel(domain, appKey, openId, nonce, timestamp, signature);
	}


	public static class ImSignModel {

		private String domain;
		private String appKey;
		private Long openId;
		private String nonce;
		private Long timestamp;
		private String signature;

		public ImSignModel(){

		}

		public ImSignModel(String domain, String appKey, Long openId, String nonce, Long timestamp,
						   String signature) {
			super();
			this.domain = domain;
			this.appKey = appKey;
			this.openId = openId;
			this.nonce = nonce;
			this.timestamp = timestamp;
			this.signature = signature;
		}

		/**
		 * @return the domain
		 */
		public String getDomain() {
			return domain;
		}

		/**
		 * @param domain the domain to set
		 */
		public void setDomain(String domain) {
			this.domain = domain;
		}

		/**
		 * @return the appKey
		 */
		public String getAppKey() {
			return appKey;
		}

		/**
		 * @param appKey the appKey to set
		 */
		public void setAppKey(String appKey) {
			this.appKey = appKey;
		}

		/**
		 * @return the openId
		 */
		public Long getOpenId() {
			return openId;
		}

		/**
		 * @param openId the openId to set
		 */
		public void setOpenId(Long openId) {
			this.openId = openId;
		}

		/**
		 * @return the nonce
		 */
		public String getNonce() {
			return nonce;
		}

		/**
		 * @param nonce the nonce to set
		 */
		public void setNonce(String nonce) {
			this.nonce = nonce;
		}

		/**
		 * @return the timestamp
		 */
		public Long getTimestamp() {
			return timestamp;
		}

		/**
		 * @param timestamp the timestamp to set
		 */
		public void setTimestamp(Long timestamp) {
			this.timestamp = timestamp;
		}

		/**
		 * @return the signature
		 */
		public String getSignature() {
			return signature;
		}

		/**
		 * @param signature the signature to set
		 */
		public void setSignature(String signature) {
			this.signature = signature;
		}


	}
}
